package com.mes.system.manager;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.bdf2.core.model.RoleMember;
import com.bstek.dorado.data.entity.EntityUtils;
import com.mes.common.manager.BasicManager;
import com.mes.common.util.IdentityGenerator;
import com.mes.system.dao.iface.IEmployeeRoleDao;
import com.mes.system.domain.EmployeeRole;
import com.mes.system.manager.iface.IEmployeeRoleManager;

@Component
public class EmployeeRoleManager extends BasicManager<EmployeeRole> implements IEmployeeRoleManager {
	
	@Autowired
	private IEmployeeRoleDao employeeRoleDao;
	
	@Override
	public void saveEmpRole(List<EmployeeRole> employeeRoles) throws Exception {
		for (EmployeeRole employeeRole : employeeRoles) {
			RoleMember roleMember = null;
			
			switch (EntityUtils.getState(employeeRole)) {
			case MODIFIED:

				break;
			case DELETED:
				roleMember = employeeRoleDao.getRoleMemberByEmpRole(employeeRole.getRoleUid(),
						employeeRole.getEmployeeUid());
				
				employeeRoleDao.deleteRoleMember(roleMember);
				employeeRoleDao.delete(employeeRole);
				
				break;
			case NEW:
				roleMember = new RoleMember();
				
				roleMember.setId(IdentityGenerator.GetIdentityUUID());
				roleMember.setGranted(true);
				roleMember.setCreateDate(new Date());
				roleMember.setRoleId(employeeRole.getRoleUid());
				roleMember.setUsername(employeeRole.getEmployeeUid());
				
				employeeRoleDao.saveRoleMember(roleMember);

				employeeRoleDao.add(employeeRole);
				break;
			default:
				break;
			}
		}
		
	}
}
